﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKRevitDimensionText.DimCreator;
using YJKRevitDimensionText.OneKeyPlanDim.DimesionDataEx;
using YJKRevitDimensionText.OneKeyPlanDim.InWallDim;
using YJKRevitDimensionText.OneKeyPlanDim.OutWallThreeDim;
using YJKRevitDimensionText.OneKeyPlanDim.Utility.XDataUtils;

namespace YJKRevitDimensionText.OneKeyPlanDim
{
	public class DrawPlanDim
	{
		public DrawPlanDim(UIApplication uiApp)
		{
			this.m_uiApp = uiApp;
			this.m_doc = this.m_uiApp.ActiveUIDocument.Document;
			this.m_BaseData = new BaseDataOper(uiApp);
			DimAvoidManager.m_lstAllDoorWinInfo.AddRange(this.m_BaseData.GetInWallData());
			DimAvoidManager.m_lstAllDoorWinInfo.AddRange(this.m_BaseData.GetOutWallData());
			this.ClearOneKeyPlanDim();
		}

		public void DrawInWallDWDimFun()
		{
			DrawInWallDWDim drawInWallDWDim = new DrawInWallDWDim(this.m_doc, this.m_BaseData);
			drawInWallDWDim.DrawDim();
			this.m_lstCrossWallId = drawInWallDWDim.DimCrossWall;
		}

		public void DrawOutWallDWDimFun()
		{
			if (this.m_lstNotDimWinDoor.Count < 1)
			{
				return;
			}
			new DrawInWallDWDim(this.m_doc, this.m_BaseData, this.m_lstOutWallData, this.m_lstNotDimWinDoor).DrawDim();
		}

		public void DrawInWallThickDimFun()
		{
			new DrawInWallThickDim(this.m_doc, this.m_BaseData, this.m_lstCrossWallId, this.m_dicWallDim).DrawDim();
		}

		public void DrawThreeDim()
		{
			List<ElementId> list = new List<ElementId>();
			new OutWallThreeDimManagerEx(this.m_uiApp, this.m_doc, this.m_doc.ActiveView).GetAllThreeDim(this.m_BaseData, ref list);
			List<WallDataEx> outWallData = this.m_BaseData.GetOutWallData();
			new OutWallThreeDimManager(this.m_doc, this.m_uiApp, this.m_doc.ActiveView, outWallData).CreateOutWallThreeDimesion(ref this.m_dicWallDim);
			foreach (WallDataEx wallDataEx in this.m_BaseData.GetOutWallData())
			{
				List<ElementId> list2 = new List<ElementId>();
				foreach (DoorWinDataEx doorWinDataEx in wallDataEx.WallWindows)
				{
					if (!list.Contains(doorWinDataEx.DWId))
					{
						list2.Add(doorWinDataEx.DWId);
					}
				}
				if (list2.Count > 0)
				{
					this.m_lstOutWallData.Add(wallDataEx);
					this.m_lstNotDimWinDoor.AddRange(list2);
				}
			}
		}

		public void DrawOTherDim()
		{
		}

		public void ClearOneKeyPlanDim()
		{
			List<Dimension> list = new FilteredElementCollector(this.m_doc, this.m_doc.ActiveView.Id).OfClass(typeof(Dimension)).Cast<Dimension>().ToList<Dimension>();
			List<Dimension> list2 = new List<Dimension>();
			foreach (Dimension dimension in list)
			{
				if (ElementStringXDataUtil.IsOneKeyPlaneDim(dimension))
				{
					list2.Add(dimension);
				}
			}
			using (Transaction transaction = new Transaction(this.m_doc, "delOneKeyPlanDim"))
			{
				transaction.Start();
				try
				{
					foreach (Dimension dimension2 in list2)
					{
						this.m_doc.Delete(dimension2.Id);
					}
					transaction.Commit();
				}
				catch (Exception)
				{
					transaction.RollBack();
				}
			}
		}

		[Conditional("DEBUG")]
		private void ShowOutWallInfo()
		{
			Transaction transaction = new Transaction(this.m_doc);
			try
			{
				transaction.Start("测试获取外墙数据");
				foreach (KeyValuePair<GridGroupDataEx, List<WallDataMerge>> keyValuePair in WallOperattionUtil.GetOutWallDataByGrid(this.m_uiApp, this.m_BaseData))
				{
					GridGroupDataEx key = keyValuePair.Key;
					List<WallDataMerge> value = keyValuePair.Value;
				}
				transaction.Commit();
			}
			catch (Exception ex)
			{
				transaction.RollBack();
				Trace.Assert(false, ex.Message);
			}
		}

		[Conditional("DEBUG")]
		private void DrawGridCurve(GridGroupDataEx gridGroup)
		{
            Autodesk.Revit.DB.Color color = new Autodesk.Revit.DB.Color(byte.MaxValue, 0, 0);
			foreach (Line curCurve in gridGroup.LstLine)
			{
				CurveDrawing.CreateModelCurve(curCurve, this.m_doc, color, 1);
			}
		}

		[Conditional("DEBUG")]
		private void NoteOutWallData(List<WallDataMerge> lstWallData)
		{
            Autodesk.Revit.DB.Color color = new Autodesk.Revit.DB.Color(0, 0, byte.MaxValue);
			foreach (WallDataMerge wallDataMerge in lstWallData)
			{
				CurveDrawing.CreateModelCurve(wallDataMerge.MergeProfileLine, this.m_doc, color, 1);
			}
		}

		private BaseDataOper m_BaseData;

		private Document m_doc;

		private UIApplication m_uiApp;

		private List<ElementId> m_lstNotDimWinDoor = new List<ElementId>();

		private List<WallDataEx> m_lstOutWallData = new List<WallDataEx>();

		public List<WallDataEx> m_lstAllWallDataEx = new List<WallDataEx>();

		private List<ElementId> m_lstCrossWallId = new List<ElementId>();

		private Dictionary<ElementId, ElementId> m_dicWallDim = new Dictionary<ElementId, ElementId>();
	}
}
